<?php

/*
 * 微信公众平台操作类
 */
namespace Gaoming13\WechatPhpSdk;
use Think\Log;
class WeChatewm {
	
	private $_appid;
	private $_appsecret;
	
	//表示QRCode的类型
	const QRCODE_TYPE_TEMP = 1;
	const QRCODE_TYPE_LIMIT = 2;
	const QRCODE_TYPE_LIMIT_STR = 3;
	
	
	public function __construct($id,$secret){
		$this->_appid = $id;
		$this->_appsecret = $secret;
	}
	
	/**
	 *获取二维码ticket 
	 *@param	$content	内容
	 *@param	$type		QR码类型（1：临时，2：永久，3：永久字符串）
	 *@param	$expire		有效期，临时的QR码就可以更改该参数
	 */
	private function _getQRCodeTicket($content,$type=1,$expire=2592000,$token){
		$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$token";
		$type_list = array(
			self::QRCODE_TYPE_TEMP => 'QR_SCENE',
			self::QRCODE_TYPE_LIMIT => 'QR_LIMIT_SCENE',
			self::QRCODE_TYPE_LIMIT_STR => 'QR_LIMIT_STR_SCENE'
			);
		$action_name = $type_list[$type];
		switch($type){
			case self::QRCODE_TYPE_TEMP:
				$data_arr['action_name'] = $action_name;
				$data_arr['expire_seconds'] = $expire;
				$data_arr['action_info']['scene']['scene_id'] = $content;
				break;
			case self::QRCODE_TYPE_LIMIT:
				$data_arr['action_name'] = $action_name;
				$data_arr['action_info']['scene']['scene_id'] = $content;
				break;
			case self::QRCODE_TYPE_LIMIT_STR:
				$data_arr['action_name'] = $action_name;
				$data_arr['action_info']['scene']['scene_str'] = "$content";
				break;		
		}
		$data = json_encode($data_arr); 
		$result = $this->_requestPost($url,$data);
		if(!$result){
			return FALSE;
		}
		//存在处理响应数据
		$result_obj = json_decode($result);
		
		//var_dump($result_obj);echo "<br>";
		
		return $result_obj->ticket;
	}
	
	/**
	 *获取二维码ticket 
	 *@param	$content	内容int/string
	 *@param  	$file		储存为文件的地址
	 *@param	$type		QR码类型（1：临时，2：永久，3：永久字符串）
	 *@param	$expire		有效期，临时的QR码就可以更改该参数int
	 */
	public function getQRCode($content,$file=NULL,$type=1,$expire=2592000,$token){
		Log::write('conkey：'.$content);
		$ticket = $this->_getQRCodeTicket($content,$type,$expire,$token);
		$ticket = UrlEncode($ticket);
		Log::write('ticket_code：'.$ticket);	
		
		$url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
		
		$result = $this->_requestGet($url);//此时result就是图像内容
		if($file&&$result){
			file_put_contents($file,$result);
		}	
			
		return $result;
		
	}
	
	private function _requestPost($url,$data,$ssl=true){
		//curl完成
		$curl = curl_init();
		//设置curl选项
		curl_setopt($curl, CURLOPT_URL, $url);
		$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0';//请求代理信息
		curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
		curl_setopt($curl, CURLOPT_AUTOREFERER, true);
		if($ssl){
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		//处理post相关任务
		curl_setopt($curl, CURLOPT_POST, true);//是否为post请求
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//处理请求数据
		//处理响应结果
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		
		//发送请求
		$response = curl_exec($curl);
		if(false===$response){
			echo curl_error($curl);
			return false;
		}
		return $response;
	}	
	
	
	
	/*
	 * 发送get请求的方法
	 * @param	string	$url	目标URL
	 * @param	bool	$ssl	是否为https协议
	 * @return	string	响应主体Content
	 */
	private function _requestGet($url,$ssl=true){
		//curl完成
		$curl = curl_init();
		//设置curl选项
		curl_setopt($curl, CURLOPT_URL, $url);
		$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0';//请求代理信息
		curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
		curl_setopt($curl, CURLOPT_AUTOREFERER, true);
		if($ssl){
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		
		//发送请求
		$response = curl_exec($curl);
		if(false===$response){
			echo curl_error($curl);
			return false;
		}
		return $response;
		
	}
	
}
